package com.acce.core.utils;

import java.io.UnsupportedEncodingException;

public class EncodingTool {  
    public static String encodeStr(String str) {  
    	if(null == str){
    		return str;
    	}
    	String encode = getEncoding(str);
        try { 
        	if("UTF-8".equals(encode)){
        		return str;
        	}else if("ISO-8859-1".equals(encode)){
        		return new String(str.getBytes("ISO-8859-1"), "UTF-8");
        	}else if("GBK".equals(encode)){
        		return str;
//        		return new String(str.getBytes("GBK"), "UTF-8");
        	}else if("GB2312".equals(encode)){
        		return str;
//        		return new String(str.getBytes("GB2312"), "UTF-8");
        	}
//        	if(str.equals(new String(str.getBytes("UTF-8"), "UTF-8"))){
//        		return str;
//            }
//        	if(str.equals(new String(str.getBytes("ISO-8859-1"), "ISO-8859-1"))){
//        		return new String(str.getBytes("ISO-8859-1"), "UTF-8");
//        	}
        	return str;
//            return new String(str.getBytes("ISO-8859-1"), "UTF-8");  
        } catch (UnsupportedEncodingException e) {  
            e.printStackTrace();  
            return str;  
        }  
    }  
    
    public static String getEncoding(String str) {      
        String encode = "GB2312";      
       try {      
           if (str.equals(new String(str.getBytes(encode), encode))) {      
                String s = encode;      
               return s;      
            }      
        } catch (Exception exception) {      
        }      
        encode = "ISO-8859-1";      
       try {      
           if (str.equals(new String(str.getBytes(encode), encode))) {      
                String s1 = encode;      
               return s1;      
            }      
        } catch (Exception exception1) {      
        }      
        encode = "UTF-8";      
       try {      
           if (str.equals(new String(str.getBytes(encode), encode))) {      
                String s2 = encode;      
               return s2;      
            }      
        } catch (Exception exception2) {      
        }      
        encode = "GBK";      
       try {      
           if (str.equals(new String(str.getBytes(encode), encode))) {      
                String s3 = encode;      
               return s3;      
            }      
        } catch (Exception exception3) {      
        }      
       return "UTF-8";      
    }      
}  
